/// Author: Samle
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class EditWordsController extends GetxController {

  /// 获取Key用来获取Form表单组件
  GlobalKey<FormState> editWordsKey = GlobalKey<FormState>();
  TextEditingController name = TextEditingController();
  TextEditingController trans = TextEditingController();
  TextEditingController ukphone = TextEditingController();
  TextEditingController ukspeech = TextEditingController();
  TextEditingController usphone = TextEditingController();
  TextEditingController usspeech = TextEditingController();
  TextEditingController syno = TextEditingController();
  TextEditingController relWord = TextEditingController();
  TextEditingController sentence = TextEditingController();
  TextEditingController phrase = TextEditingController();
  TextEditingController exam = TextEditingController();
  TextEditingController belong = TextEditingController();
  TextEditingController checkTimes = TextEditingController();
  TextEditingController errorTimes = TextEditingController();
  TextEditingController helpText = TextEditingController();
  TextEditingController soundText = TextEditingController();

  /// 数据表单
  Map dataForm = {
    "id": null,
    "name": null,
    "trans": null,
    "ukphone": null,
    "ukspeech": null,
    "usphone": null,
    "usspeech": null,
    "syno": null,
    "relWord": null,
    "sentence": null,
    "phrase": null,
    "exam": null,
    "belong": null,
    "checkTimes": null,
    "errorTimes": null,
    "helpText": null,
    "soundText": null,
  };

  /// 数据表单
  Map cleanDataForm = {
    "id": null,
    "name": null,
    "trans": null,
    "ukphone": null,
    "ukspeech": null,
    "usphone": null,
    "usspeech": null,
    "syno": null,
    "relWord": null,
    "sentence": null,
    "phrase": null,
    "exam": null,
    "belong": null,
    "checkTimes": null,
    "errorTimes": null,
    "helpText": null,
    "soundText": null,
  };

  /// 获取数据
  getData() async {
    try {
      if (Get.arguments.isNotEmpty && Get.arguments != null){
        dataForm = Get.arguments;
        name.text = dataForm['name'] != null ? dataForm['name'].toString() : '';
        trans.text = dataForm['trans'] != null ? dataForm['trans'].toString() : '';
        ukphone.text = dataForm['ukphone'] != null ? dataForm['ukphone'].toString() : '';
        ukspeech.text = dataForm['ukspeech'] != null ? dataForm['ukspeech'].toString() : '';
        usphone.text = dataForm['usphone'] != null ? dataForm['usphone'].toString() : '';
        usspeech.text = dataForm['usspeech'] != null ? dataForm['usspeech'].toString() : '';
        syno.text = dataForm['syno'] != null ? dataForm['syno'].toString() : '';
        relWord.text = dataForm['relWord'] != null ? dataForm['relWord'].toString() : '';
        sentence.text = dataForm['sentence'] != null ? dataForm['sentence'].toString() : '';
        phrase.text = dataForm['phrase'] != null ? dataForm['phrase'].toString() : '';
        exam.text = dataForm['exam'] != null ? dataForm['exam'].toString() : '';
        belong.text = dataForm['belong'] != null ? dataForm['belong'].toString() : '';
        checkTimes.text = dataForm['checkTimes'] != null ? dataForm['checkTimes'].toString() : '';
        errorTimes.text = dataForm['errorTimes'] != null ? dataForm['errorTimes'].toString() : '';
        helpText.text = dataForm['helpText'] != null ? dataForm['helpText'].toString() : '';
        soundText.text = dataForm['soundText'] != null ? dataForm['soundText'].toString() : '';
      }
    } catch (e) {
      print("数据获取失败");
    } finally {
    }
  }

  /// 返回数据
  saveData() {
    dataForm['name'] = name.text;
    dataForm['trans'] = trans.text;
    dataForm['ukphone'] = ukphone.text;
    dataForm['ukspeech'] = ukspeech.text;
    dataForm['usphone'] = usphone.text;
    dataForm['usspeech'] = usspeech.text;
    dataForm['syno'] = syno.text;
    dataForm['relWord'] = relWord.text;
    dataForm['sentence'] = sentence.text;
    dataForm['phrase'] = phrase.text;
    dataForm['exam'] = exam.text;
    dataForm['belong'] = belong.text;
    if (checkTimes.text != null && checkTimes.text != '' && checkTimes.text != 'null'){
      dataForm['checkTimes'] = int.parse(checkTimes.text);
    } else {
      dataForm['checkTimes'] = '';
    }
    if (errorTimes.text != null && errorTimes.text != '' && errorTimes.text != 'null'){
      dataForm['errorTimes'] = int.parse(errorTimes.text);
    } else {
      dataForm['errorTimes'] = '';
    }
    dataForm['helpText'] = helpText.text;
    dataForm['soundText'] = soundText.text;
    Get.back(result: dataForm);
  }

  @override
  void onInit() {
    getData();
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
